/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.elem.uml;

import java.awt.*;

import self.math.geom.*;
import self.amigo.elem.*;

public class SequenceDiagramActorView extends SequenceDiagramObjectView {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  protected ARectangularHandledElement constructDelegate() {
    return new EssentialActorView();
  }

  protected class EssentialActorView extends EssentialObjectView {
  	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
    private Rectangle figActor;

    protected void setInitialBounds() {
      super.setInitialBounds();
      figActor = new Rectangle( 10, 10, 40, 60 );
    }

    protected void resetFrame() {
      super.resetFrame();
      figActor.setLocation( ((fig.width-40) / 2) + fig.x, fig.y - 60 );
    }

    protected void paintFrame( Graphics surface ) {
      surface.setColor( frameColor );
      if ( outlineOnly ) {
        surface.drawRect( fig.x, fig.y, fig.width, fig.height );
        surface.drawRect( figActor.x, figActor.y, figActor.width, figActor.height );
      } else {
        int yPlus38 = figActor.y + 38;
        int xPlus20 = figActor.x + 20;
        int yPlus56 = figActor.y + 56;
        int yPlus27 = figActor.y + 27;

        surface.drawOval( figActor.x + 10, figActor.y, 20, 20 ); // head
        surface.drawLine( xPlus20, figActor.y + 20, xPlus20, yPlus38 ); // body
        surface.drawLine( figActor.x + 5, yPlus27, figActor.x + 35, yPlus27 ); // arm
        surface.drawLine( figActor.x + 2, yPlus56, xPlus20, yPlus38 ); // left leg
        surface.drawLine( figActor.x + 38, yPlus56, xPlus20, yPlus38 ); // right leg
      }
      textPainter.paint( surface, ctxText, outlineOnly );
    }

    public boolean    contains( Point within ) {
      boolean ret = fig.contains( within );
      if ( !ret )
        ret = figActor.contains( within );
      return ret;
    }

    public boolean    isWithin( Rectangle within ) {
      boolean ret = fig.intersects( within );
      if ( !ret )
        ret = figActor.intersects( within );
      return ret;
    }

    public Rectangle  getOverallBounds() {
      Rectangle ret = super.getOverallBounds();
      ret = ret.union( figActor );
      return ret;
    }

    public boolean getLineIntersectionToCentre( int xFrom, int yFrom, int[] xyPoints ) {
      double[] xyP = new double[2];
      if ( yFrom <= fig.y ) {  // use Actor
        RectangleUtils.getRectCentrePointIntersections( figActor, xFrom, yFrom, xyP );
        xyPoints[0] = (int)xyP[0];
        xyPoints[1] = (int)xyP[1];
        return true;
      }
      Rectangle union = new Rectangle( fig );
      union.add( figActor );
      RectangleUtils.getRectCentrePointIntersections( union, xFrom, yFrom, xyP );
      xyPoints[0] = (int)xyP[0];
      xyPoints[1] = (int)xyP[1];
      return true;
    }
  }
}
